<?php


namespace App\Customer\Controller;

use EchoPHP\Facades\Request;

/**
 * 控制器核心基类，提供基本信息等
 * Class BaseController
 * @package App\Customer\Controller
 */
abstract class BaseController {
    const STATUS_TOKEN_EXPIRED = 484;
    protected $statusMsg = [
        self::STATUS_TOKEN_EXPIRED => '访问授权已过期',
    ];


    protected $request;
    protected $currentController;
    protected $currentAction;

    /**
     * BaseController constructor.
     */
    public function __construct() {
        $this->request = Request::instance();
        $this->currentController = $this->request->getAttribute('controller');
        $this->currentAction = $this->request->getAttribute('action');
    }

    /**
     * 获取当前控制器
     * @return array|mixed|void|null
     */
    public function getCurrentController() {
        return $this->currentController;
    }

    /**
     * 获取当前操作名
     * @return array|mixed|void|null
     */
    public function getCurrentAction() {
        return strtolower($this->currentAction);
    }

    /**
     * 输出json响应
     * @param array $responseData
     * @return \EchoPHP\Http\Response\JsonResponse
     * @throws \Exception
     */
    public function json(array $responseData) {
        return response()->json($responseData);
    }

    /**
     * 控制器预处理
     * @param $method
     * @param $parameters
     * @return mixed
     */
    public function callAction($method, $parameters) {
        $method = strtolower($method);
        return $this->{$method}(...$parameters);
    }

    /**
     * 根据全局错误码获取全局错误信息
     * @param $status
     * @return mixed|string
     */
    public function getStatusMsg($status) {
        return isset($this->statusMsg[$status]) ? $this->statusMsg[$status] : '系统繁忙，请稍后再试';
    }
}